package weaver.interfaces.dsjj.duss.docCronJob;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.dsjj.duss.TransMethod;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.Arrays;
import java.util.List;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-06-05
 * @Description: 公文信息同步到中间表
 * @ModifyLog:
 **/

public class DocInfoSynMid extends BaseCronJob {
    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog("in----DocInfoSynMid");
        String curtime = TransMethod.getTime();
        String timebefore = TransMethod.getTimebefore();
        //发文信息更新到中间表
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        RecordSet rs2 = new RecordSet();
        RecordSet rs3 = new RecordSet();
        String sql = "select lcid from uf_fwngd where CONCAT(CONCAT(modedatacreatedate,' '),modedatacreatetime)  between '" + timebefore + "' and '" + curtime + "'";
        baseBean.writeLog(sql);
        rs.executeQuery(sql);
        int count1 = 0;
        while (rs.next()) {
            String requestid = Util.null2String(rs.getString("lcid"));//requestid
            rs1.executeQuery("select * from uf_fwngd where lcid = '" + requestid + "'");
            while (rs1.next()) {
                rs2.executeQuery("select count(*) as counts from document_middleinfo_send where requestid='" + requestid + "'");
                rs2.next();
                int counts = Util.getIntValue(rs2.getString("counts"));
                baseBean.writeLog("counts:" + counts);
                if (counts <= 0) {
                    String formmodeid = Util.null2String(rs1.getString("formmodeid"));//建模ID
                    String fwlx = Util.null2String(rs1.getString("fwlx"));//发文类型
                    String ngdwid = Util.null2String(rs1.getString("ngdw"));//拟稿单位
                    String ngdw = TransMethod.bmTransMethod(ngdwid);//拟稿单位
                    if ("".equals(ngdwid)) {
                        ngdwid = "0";
                        ngdw = "0";
                    }
                    String dwxz = "0";//单位性质
                    String ngrq = Util.null2String(rs1.getString("ngrq"));//拟稿日期
                    String ngrid = Util.null2String(rs1.getString("ngr"));//拟稿人
                    String ngr = TransMethod.bmTransMethod(ngrid);//	拟稿人名称
                    String ngrguid = TransMethod.transIDtoGUID(ngrid);//	拟稿人GUID
                    String fzr = Util.null2String(rs1.getString("fzr"));//负责人
                    String requestname = Util.null2String(rs1.getString("requestname"));//名称
                    String fwzh = Util.null2String(rs1.getString("fwzh"));//发文字号
                    String bmqx = Util.null2String(rs1.getString("bmqx"));//保密期限
                    String dmzrr = Util.null2String(rs1.getString("dmzrr"));//负责人
                    String dmyj = Util.null2String(rs1.getString("dmyj"));//定密责任人
                    String fs = Util.null2String(rs1.getString("fs"));//份数
                    String zw = Util.null2String(rs1.getString("zw"));//正文
                    String zwfj = Util.null2String(rs1.getString("zwfj"));//正文附件
                    String PJCLD = Util.null2String(rs1.getString("PJCLD"));//签批单
                    String zsfw = Util.null2String(rs1.getString("zsfw"));//主送范围
                    String csfw = Util.null2String(rs1.getString("csfw"));//抄送范围
                    String bz = Util.null2String(rs1.getString("bz"));//备注
                    String sqld = Util.null2String(rs1.getString("sqld"));//审签领导

                    String sql1 = "insert into document_middleinfo_send(formmodeid,fwlx,ngdwid,ngdw,dwxz,ngrq,ngrid,ngr,ngrguid,fzr,requestname,fwzh,bmqx,dmzrr,dmyj,fs," +
                            "requestid,zw,zwfj,PJCLD,zsfw,csfw,bz,sqld) " +
                            "values('" + formmodeid + "','" + fwlx + "','" + ngdwid + "','" + ngdw + "','" + dwxz + "','" + ngrq + "','" + ngrid + "','" + ngr + "','" + ngrguid + "','" + fzr + "','" + requestname + "','" + fwzh + "','" + bmqx + "','" + dmzrr + "','" +
                            dmyj + "','" + fs + "','" + requestid + "','" + zw + "','" + zwfj + "','" + PJCLD + "','" + zsfw + "','" + csfw + "','" + bz + "','" + sqld + "')";
                    rs1.executeUpdate(sql1);
                    if (!"".equals(zw)) {
                        rs3.executeQuery("select imagefileid,imagefilename,filerealpath,iszip,filesize from imagefile where imagefileid in (select imagefileid from docimagefile where  isextfile ='1' and docid = '" + zw + "')");
                        while (rs3.next()) {
                            String imagefileid = Util.null2String(rs3.getString("imagefileid"));
                            String imagefilename = Util.null2String(rs3.getString("imagefilename"));
                            String iszip = Util.null2String(rs3.getString("iszip"));
                            String filesize = Util.null2String(rs3.getString("filesize"));
                            String filepath = Util.null2String(rs3.getString("filerealpath"));
                            String insert_sql = "insert into document_middleinfo_file(requestid,fileid,filename,filesize,iszip,filecontent) values('" + requestid + "','" + imagefileid + "','" + imagefilename + "','" + filesize + "','" + iszip + "','EMPTY_BLOB()')";
                            if (rs3.executeUpdate(insert_sql)) {
                                String sql2 = " update document_middleinfo_file set filecontent=? where fileid=?";
                                TransMethod.getDoc2Clob(filepath, sql2, iszip, imagefileid);
                            }
                        }
                    }
                    count1++;
                } else {
                    continue;
                }
            }

        }
        baseBean.writeLog("当前同步发文数：[" + count1 + "]");
        //收文信息更新到中间表
        baseBean.writeLog("---开始同步收文---");
        String SQL = "select lcid from uf_swdjd where CONCAT(CONCAT(modedatacreatedate,' '),modedatacreatetime) between '" + timebefore + "' and '" + curtime + "'";
        baseBean.writeLog(SQL);
        rs.executeQuery(SQL);
        int count = 0;
        while (rs.next()) {
            String requestid = Util.null2String(rs.getString("lcid"));//requestid
            rs1.executeQuery("select * from uf_swdjd where lcid = '" + requestid + "'");
            while (rs1.next()) {
                rs2.executeQuery("select count(*) as counts from document_middleinfo_resource where requestid='" + requestid + "'");
                rs2.next();
                int counts = Util.getIntValue(rs2.getString("counts"));
                baseBean.writeLog("counts:" + counts);
                if (counts <= 0) {
                    String formmodeid = Util.null2String(rs1.getString("formmodeid"));//建模ID
                    String lcbh = Util.null2String(rs1.getString("lcbh"));//流程编号
                    String swdwid = Util.null2String(rs1.getString("swdw"));//收文单位ID
                    String swdw = TransMethod.bmTransMethod(swdwid);//收文单位
                    String swyid = Util.null2String(rs1.getString("swy"));//收文员ID
                    String swy = TransMethod.transIDtoLastname(swyid);//收文员
                    String swyguid = TransMethod.transIDtoGUID(swyid);//收文员GUID
                    String swsj = Util.null2String(rs1.getString("swsj"));//收文时间
                    String sh = Util.null2String(rs1.getString("sh"));//审核
                    String zb = Util.null2String(rs1.getString("zb"));//主办
                    String dh = Util.null2String(rs1.getString("dh"));//电话
                    String requestname = Util.null2String(rs1.getString("requestname"));//名称
                    String lwzh = Util.null2String(rs1.getString("lwzh"));//来文字号
                    String lwdwid = Util.null2String(rs1.getString("lwdw"));//来文单位
                    String lwdw = "";
                    if ("".equals(lwdwid)) {
                        lwdwid = "0";
                        lwdw = Util.null2String(rs1.getString("lwdwa"));//来文单位文本
                    } else {
                        lwdw = TransMethod.bmTransMethod(lwdwid);//来文单位
                    }
                    String mj = Util.null2String(rs1.getString("mj"));//密级
                    String blqk = Util.null2String(rs1.getString("blqk"));//办理情况
                    String fwfw = Util.null2String(rs1.getString("fwfw"));//发文范围
                    String sfjs = Util.null2String(rs1.getString("sfjs"));//是否结束
                    String psld = Util.null2String(rs1.getString("psld"));//批示领导
                    String blbm = Util.null2String(rs1.getString("blbm"));//办理部门
                    String xgwd = Util.null2String(rs1.getString("xgwd"));//相关文档
                    String xglc = Util.null2String(rs1.getString("xglc"));//相关流程
                    String xgfj = Util.null2String(rs1.getString("xgfj"));//相关附件
                    String zwfj = Util.null2String(rs1.getString("zwfj"));//正文附件
                    String sql1 = "insert into document_middleinfo_resource( formmodeid,requestid,lcbh,swdwid,swdw,swyid,swy,swyguid,swsj,sh,zb,dh,requestname,lwzh,lwdwid,lwdw,mj," +
                            "blqk,fwfw,sfjs,psld,blbm,xgwd,xglc,xgfj,zwfj) values('" + formmodeid + "','" + requestid + "','" + lcbh + "','" + swdwid + "','" + swdw + "','" + swyid + "','" + swy + "','" + swyguid + "','" + swsj + "','" + sh + "','" + zb + "','" + dh + "','"
                            + requestname + "','" + lwzh + "','" + lwdwid + "','" + lwdw + "','" + mj + "','" + blqk + "','" + fwfw + "','" + sfjs + "','" + psld + "','" + blbm + "','" + xgwd + "','" + xglc + "','" + xgfj + "','" + zwfj + "')";
                    rs.executeUpdate(sql1);
                    if (!"".equals(zwfj)) {//多附件
                        if (zwfj.contains(",")) {
                            List<String> result = Arrays.asList(zwfj.split(","));
                            for (int i = 0; i < result.size(); i++) {
                                String docid = result.get(i);
                                rs3.executeQuery("select imagefileid,imagefilename,filerealpath,iszip,filesize from imagefile where imagefileid in (select imagefileid from docimagefile where docid = '" + docid + "')");
                                while (rs3.next()) {
                                    String imagefileid = Util.null2String(rs3.getString("imagefileid"));
                                    String imagefilename = Util.null2String(rs3.getString("imagefilename"));
                                    String iszip = Util.null2String(rs3.getString("iszip"));
                                    String filesize = Util.null2String(rs3.getString("filesize"));
                                    String filepath = Util.null2String(rs3.getString("filerealpath"));
                                    String insert_sql = "insert into document_middleinfo_file(requestid,fileid,filename,filesize,iszip,filecontent) values('" + requestid + "','" + imagefileid + "','" + imagefilename + "','" + filesize + "','" + iszip + "','EMPTY_BLOB()')";
                                    if (rs3.executeUpdate(insert_sql)) {
                                        String sql2 = " update document_middleinfo_file set filecontent=? where fileid=?";
                                        TransMethod.getDoc2Clob(filepath, sql2, iszip, imagefileid);
                                    }
                                }
                            }
                        } else {//单附件
                            String docid = zwfj;
                            rs3.executeQuery("select imagefileid,imagefilename,filerealpath,iszip,filesize from imagefile where imagefileid in (select imagefileid from docimagefile where docid = '" + docid + "')");
                            while (rs3.next()) {
                                String imagefileid = Util.null2String(rs3.getString("imagefileid"));
                                String imagefilename = Util.null2String(rs3.getString("imagefilename"));
                                String iszip = Util.null2String(rs3.getString("iszip"));
                                String filesize = Util.null2String(rs3.getString("filesize"));
                                String filepath = Util.null2String(rs3.getString("filerealpath"));
                                String insert_sql = "insert into document_middleinfo_file(requestid,fileid,filename,filesize,iszip,filecontent) values('" + requestid + "','" + imagefileid + "','" + imagefilename + "','" + filesize + "','" + iszip + "','EMPTY_BLOB()')";
                                if (rs3.executeUpdate(insert_sql)) {
                                    String sql2 = " update document_middleinfo_file set filecontent=? where fileid=?";
                                    TransMethod.getDoc2Clob(filepath, sql2, iszip, imagefileid);
                                }
                            }
                        }
                    }
                    count++;
                } else {
                    continue;
                }
            }
            baseBean.writeLog("收文更新到中间表记录数[" + count + "]");
        }
        rs.executeUpdate("update syntimedocinfo set beforetime = '"+timebefore+"',curtime='"+curtime+"' where id ='1'");
    }

}


